package com.blb.servlet;


import com.blb.utils.ConfigUtils;
import com.blb.utils.IOUtils;
import com.blb.utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.Collection;
import java.util.UUID;

/**
 * 文件、图片上传下载
 */
@MultipartConfig
@WebServlet("/common/*")
public class CommonServlet extends BaseServlet {
    /**
     * 在后台显示轮播图图片
     * @param req
     * @param resp
     */
    public void getImage(HttpServletRequest req, HttpServletResponse resp){
        try {
            String image = req.getParameter("image");
            resp.setContentType("image/JPEG");
//            FileInputStream in = new FileInputStream("C:/upload/eshop/"  + image);
            FileInputStream in = new FileInputStream(ConfigUtils.getUploadPath() + image);
            ServletOutputStream out = resp.getOutputStream();
            IOUtils.write(in, out);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void upload(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            Collection<Part> parts = req.getParts();
            String fileNames="";

            for (Part part : parts) {
                String name = part.getSubmittedFileName();
                String suffix = name.substring(name.lastIndexOf("."));
                String fileName = UUID.randomUUID().toString() + suffix;

                InputStream in = part.getInputStream();
                FileOutputStream out = new FileOutputStream(ConfigUtils.getUploadPath() + fileName);
//                FileOutputStream out = new FileOutputStream("D:/upload/eshop/" + fileName);
                IOUtils.write(in, out);
                fileNames+=","+fileName;
            }
            if (!fileNames.equals("")){
                fileNames=fileNames.substring(1);
            }
            resp.getWriter().write(fileNames);
        } catch (ServletException | IOException e) {
            resp.getWriter().write("err");
            throw new RuntimeException(e);
        }
    }

    /**
     * 单独上传
     * @param req
     * @param resp
     * @throws IOException
     */
    public void uploadOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            //获取临时文件对象
            Part part = req.getPart("file");
            //获取此文件名
            String name = part.getSubmittedFileName();
            //获取此文件后缀
            String s = name.substring(name.lastIndexOf("."));
            //随机获取uuid并加上文件后缀
            String uuId = UUIDUtils.getId() + s;

            InputStream in = part.getInputStream();
            FileOutputStream out = new FileOutputStream(ConfigUtils.getUploadPath() + uuId);
//            FileOutputStream out = new FileOutputStream("C:/upload/eshop/" + uuId);
            IOUtils.write(in, out);

            resp.getWriter().write(uuId);
        } catch (IOException | ServletException e) {
            resp.getWriter().write("error");
            throw new RuntimeException(e);
        }
    }

}
